<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Hash;
use Illuminate\Http\Request;

class UserController extends Controller {
	/**
	 * 显示用户详情
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index(Request $request) {
		$users = User::where(function ($query) use ($request) {
			if (!empty($request->username)) {
				$query->where('username', 'like', '%' . $request->username . '%');
			}
			if (!empty($request->email)) {
				$query->where('email', $request->email);
			}
			if (!empty($request->phone)) {
				$query->where('phone', $request->phone);
			}
		})->paginate($request->input('num', 10));
		return view('admin.user.index', ['users' => $users, 'title' => '用户列表', 'request' => $request]);
	}

	/**
	 * 显示用户添加页面
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create() {
		return view('admin.user.create', ['title' => '用户添加']);
	}

	/**
	 * 执行用户添加
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(Request $request) {
		$this->validate($request, [
			'username' => 'required|regex:/^\w{5,18}$/',
			'password' => 'required|regex:/^\S{5,20}$/',
			'status' => 'required',
			'auth' => 'required',
		], [
			'username.regex' => '用户名不符合规则',
			'username.required' => '用户名不能为空',
			'status' => '用户状态不合法',
			'auth' => '用户权限不合法',
		]);

		//实例化模型对象
		$user = new User;

		//获取数据
		$user->username = $request->username;
		$user->password = Hash::make($request->password);
		$user->status = $request->status;
		$user->auth = $request->auth;
		$user->face = '/uploads/temp.jpg';
		//保存数据
		// $res = $user->save();
		if ($user->save()) {
			$user->datail()->user_id = $user->id;
			$user->save();
			return redirect('/admin/user')->with('success', 1);
		} else {
			return back();
		}
	}

	/**
	 * 用户编辑页面
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id) {
		$user = User::findOrFail($id);
		return view('admin.user.edit', ['title' => '用户编辑', 'user' => $user]);
	}

	/**
	 * 执行用户信息更新
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(Request $request, $id) {
		//表单验证
		$this->validate($request, [
			'username' => 'required|regex:/^\w{8,18}$/',
			'email' => 'required|regex:/^\w{3,10}@\w{1,5}\.\w{2,8}$/',
			'phone' => 'required|regex:/^1\d{10}$/',
			'status' => 'required',
			'auth' => 'required',
		], [
			'username.regex' => '用户名不符合规则',
			'username.required' => '用户名不能为空',
			'email.regex' => '邮箱不符合规范',
			'phone' => '手机号不符合规范',
			'status' => '用户状态不合法',
			'auth' => '用户权限不合法',
		]);
		$user = User::findOrFail($id);
		$user->username = $request->username;
		$user->email = $request->email;
		$user->phone = $request->phone;
		$user->status = $request->status;
		$user->auth = $request->auth;
		if ($request->hasFile('face')) {
			//上传文件夹的路径
			$dirname = './uploads/' . date('Ymd') . '/';
			//获取文件的后缀
			$suffix = $request->file('face')->getClientOriginalExtension();
			//文件名
			$name = time() . rand(100000, 999999) . '.' . $suffix;
			$request->file('face')->move($dirname, $name);
			//存入对象中(数据表中)
			$user->face = trim($dirname, '.') . $name;
		}
		// dd($user);
		// $user->face = '/uploads/default.jpg';
		// $res = $user->save();
		// dd($res);
		if ($user->save()) {
			return redirect('/admin/user')->with('success', 1);
		} else {
			return back();
		}
	}

	/**
	 * 用户删除
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id) {
		$user = User::find($id);
		//表单请求返回结果
		if ($user->delete()) {
			return back()->with('success', 1);
		} else {
			return back()->with('error', 1);
		}
	}
}
